package Common

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"math"
	"net/http"
	"strconv"
)

type StrconvRes struct {
	Status int
	Inter  int
	Msg    string
}

type aver struct {
	str  string
	it32 int32
	it64 int64
}

// Strconv string转int
func Strconv(str string) StrconvRes {
	var res StrconvRes
	inter, err := strconv.Atoi(str)
	if err != nil {
		res.Status = ERROR
		res.Inter = 0
		res.Msg = "转码失败"
	} else {
		res.Status = SUCCESS
		res.Inter = inter
		res.Msg = ""
	}
	return res
}

// MergedMapInterface 城市树形结构
func MergedMapInterface(aMap []map[string]interface{}) []map[string]interface{} {
	fmt.Println("tree -----------------------------")
	a := int64(1)
	var data []map[string]interface{}
	for _, v := range aMap {
		if v["leveltype"] == a {
			data = append(data, getChildren(aMap, v))
		}
	}
	return data
}

// getChildren 城市树形结构 二级
func getChildren(cities []map[string]interface{}, city map[string]interface{}) map[string]interface{} {
	var children []map[string]interface{}
	for _, child := range cities {
		//fmt.Printf("id: %T, - parentid: %T\n", city["id"], child["parentid"])
		id := averConvert(city["id"])
		if int64(id.it32) == child["parentid"] {
			children = append(children, getChildren(cities, child))
		}
	}
	city["children"] = children
	return city
}

// averConvert 将interface{} 转为原类型 列入 原interface 类型是int32 则转为int32，和interface再也没有关系了
func averConvert(value interface{}) aver {
	var ra aver
	switch value.(type) {
	case string:
		// 将interface转为string字符串类型
		op, ok := value.(string)
		if ok {
			ra.str = op
		}
	case int32:
		// 将interface转为int32类型
		op, ok := value.(int32)
		if ok {
			ra.it32 = op
		}
	case int64:
		// 将interface转为int64类型
		op, ok := value.(int64)
		if ok {
			ra.it64 = op
		}
	}
	return ra
}

// 当前时间 和 制定时间相隔 年月日
func TimeDiff(beginTime, endTime int64) map[string]int64 {
	startTime, endTime := int64(0), int64(0)
	if beginTime < endTime {
		startTime = beginTime
		endTime = endTime
	} else {
		startTime = endTime
		endTime = beginTime
	}

	// 计算时间差
	timediff := endTime - startTime
	days := int64(math.Floor(float64(timediff) / 86400))
	remain := timediff % 86400
	hours := int64(math.Floor(float64(remain) / 3600))
	remain = remain % 3600
	mins := int64(math.Floor(float64(remain) / 60))
	secs := remain % 60
	res := map[string]int64{
		"day":  days,
		"hour": hours,
		"min":  mins,
		"sec":  secs,
	}
	return res
}

// post 请求
// func SendPostRequest(url string, headers map[string]string, body interface{}) (*http.Response, error) {
func SendPostRequest(url string, headers map[string]string, body interface{}) string {

	jsonData, err := json.Marshal(body)

	if err != nil {
		panic(err)
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		panic(err)
	}

	for key, value := range headers {
		req.Header.Set(key, value)
	}
	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	contents := make([]byte, 0)
	if resp.StatusCode == http.StatusOK {
		contents, err = io.ReadAll(resp.Body)
		if err != nil {
			panic(err)
		}
	}
	return string(contents)
}

// 发送get请求
func sendGetRequest(url string, headers map[string]string) (*http.Response, error) {

	req, err := http.NewRequest("GET", url, nil)

	if err != nil {
		return nil, err
	}

	for key, val := range headers {
		req.Header.Set(key, val)
	}

	client := &http.Client{}
	return client.Do(req)
}
